<%@ page language="java" contentType="text/html; charset=windows-1255"
	pageEncoding="windows-1255"
	import="com.optimus.dao.CustomerDAO, com.optimus.model.mapped.CustomerBean,
	javax.servlet.http.Cookie, com.optimus.utils.SpringUtils"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="customerUser" class="com.optimus.model.mapped.CustomerBean" scope="session"></jsp:useBean>
<jsp:setProperty property="*" name="customerUser"/>
<html>
<head>
	<meta http-equiv="Content-Type"	content="text/html; charset=windows-1255">
	<meta name="description" content="Sliding Login Panel using jQuery 1.3.2" />
	<meta name="keywords" content="jquery, sliding, toggle, slideUp, slideDown, login, login form, register" />
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	
	<!-- stylesheets -->
	<link rel="stylesheet" href="css/sliderStyle.css" type="text/css" media="screen" />
	<link rel="stylesheet" href="css/slide.css" type="text/css" media="screen" />
	
	<!-- PNG FIX for IE6 -->
	<!-- http://24ways.org/2007/supersleight-transparent-png-in-ie6 -->
	<!--[if lte IE 6]>
			<script type="text/javascript" src="js/pngfix/supersleight-min.js"></script>
		<![endif]-->
	
	<!-- jQuery - the core -->
	<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
	<!-- Sliding effect -->
	<script src="js/slide.js" type="text/javascript"></script>

</head>

<body>
<%
	String helloMsg = "Welcome Guest";
	CustomerDAO customerDAO = (CustomerDAO)SpringUtils.getBean("customerDAO");
	if (request.getParameter("fullname") != null)
	{
		helloMsg = "Welcome " + request.getParameter("fullname");
		CustomerBean currentCustomer = new CustomerBean(1, request.getParameter("fullname"), request
				.getParameter("username"), request.getParameter("password"), request
				.getParameter("email"), false);
		customerDAO.insertIntoDB(currentCustomer);
		customerUser = currentCustomer;
		session.removeAttribute("customerUser");
		session.setAttribute("customerUser",currentCustomer);
	}
	else if (request.getParameter("log") != null) 
	{
		CustomerBean currentCustomer = customerDAO.selectCustomerByUsername(request.getParameter("log"));
		if (currentCustomer != null)
		{
			if ((request.getParameter("pwd") != null) && (request.getParameter("pwd").equals(currentCustomer.getPassword())))
			{
				helloMsg = "Welcome back " + currentCustomer.getFullname();
				if ((request.getParameter("rememberme") != null) && (request.getParameter("rememberme").equals("true")))
				{
					response.addCookie(new Cookie("log",request.getParameter("log")));
					response.addCookie(new Cookie("pwd",request.getParameter("pwd")));
				}
				customerUser = currentCustomer;
				session.removeAttribute("customerUser");
				session.setAttribute("customerUser",currentCustomer);
			}
		}
	}
	else
	{
		String username = null;
		String password = null;
		Cookie [] cookies = request.getCookies();
		if (cookies != null)
		{
			for (Cookie cookie : cookies)
			{
				if (cookie.getName().equals("log"))
				{
					 username= cookie.getValue();
				}
				if (cookie.getName().equals("pwd"))
				{
					password = cookie.getValue();
				}
				if ((username != null)&&(password != null))
				{
					break;
				}
			}
			CustomerBean currentCustomer = customerDAO.selectCustomerByUsername(username);
			if (currentCustomer != null)
			{
				if (currentCustomer.getPassword().equals(password))
				{
					helloMsg = "Welcome Back " + currentCustomer.getFullname();
					customerUser = currentCustomer;
					session.removeAttribute("customerUser");
					session.setAttribute("customerUser",currentCustomer);
				}
			}
		}
	}
%>
	<!-- Panel -->
	<div id="toppanel">
		<div id="panel">
			<div class="content clearfix">
				<div class="left">
				<h2>Welcome to OptimuStore !</h2>
				Check out our last twitt, you might get lucky!
				<script src="http://widgets.twimg.com/j/2/widget.js"></script>
				<script>
				new TWTR.Widget({
				  version: 2,
				  type: 'profile',
				  rpp: 1,
				  interval: 6000,
				  width: 290,
				  height: 100,
				  theme: {
				    shell: {
				      background: '#333333',
				      color: '#ffffff'
				    },
				    tweets: {
				      background: '#000000',
				      color: '#ffffff',
				      links: '#4aed05'
				    }
				  },
				  features: {
				    scrollbar: false,
				    loop: false,
				    live: false,
				    hashtags: true,
				    timestamp: true,
				    avatars: false,
				    behavior: 'all'
				  }
				}).render().setUser('zivlevy1').start();
				</script>
				<center>
					<p class="grey">
						<a href="http://www.twitter.com/zivlevy1"> 
							<img height="35" width="200" src="http://www.vistaprint.com/vp/images/b09/misc-graphics/social_networking/Twitter-234x60-001.png" alt="Follow Us on Twitter" /> 
						</a>
					</p>
				</center>
				
				</div>
				<div class="left"><!-- Login Form -->
					<form class="clearfix" action="#" method="post">
						<h3>Member Login</h3>
						<label class="grey" for="log">Username:</label>
						<input class="field" type="text" name="log" id="log" value="" size="23"/>
						<label class="grey" for="pwd">Password:</label>
						<input class="field" type="password" name="pwd" id="pwd" size="23"/>
						<label>
							<input name="rememberme" id="rememberme" type="checkbox" checked="checked"
							value="true"/> &nbsp;Remember me
						</label>
						<div class="clear"></div>
					<input type="submit" name="submit" value="Login" class="bt_login" />
					<!--<a class="lost-pwd" href="#">Lost your password?</a>-->
                    </form>                             
                    <%
                    	if (customerUser != null)
                    	{
                          if (customerUser.isAdmin())
                          {
                                out.print("<form action=\"adminDB.jsp\" method=\"post\">");
                                out.print("<input type=\"hidden\" name=\"customer\" value=\"customerID\"/>");
                                out.print("<input type=\"submit\" name=\"submit\" value=\"Admin\" class=\"bt_login\" />");
                                out.print("</form>");
                          }
                    	}
                    %>

				</div>
				<div class="left right"><!-- Register Form -->
					<form action="index.jsp" method="post"><label class="white"
						for="signup">Not a member yet? Sign in!</label> <label class="grey"
						for="signup">Full name: *</label> <input class="field" type="text"
						name="fullname" id="fullname" value="" size="23" /> <label
						class="grey" for="signup">Username: *</label> <input class="field"
						type="text" name="username" id="signup" value="" size="23" /> <label
						class="grey" for="email">Password: *</label> <input class="field"
						type="text" name="password" id="password" size="23" /> <label
						class="grey" for="email">Email: *</label> <input class="field"
						type="text" name="email" id="email" size="23" /> <!-- <label>A password will be e-mailed to you.</label> -->
					<input type="submit" name="submit" value="Register" class="bt_register" />
				
					</form>
				</div>
			</div>
		</div>
	<!-- /login --> <!-- The tab on top -->
	
	<div class="tab">
	<ul class="login">
		<li class="left">&nbsp;</li>
		<li><%=helloMsg%>!</li>
		<li class="sep">|</li>
		<li id="toggle"><a id="open" class="open" href="#">Log In |
		Register</a> <a id="close" style="display: none;" class="close" href="#">Close
		Panel</a></li>
		<li class="right">&nbsp;</li>
	</ul>
	</div>
</body>
</html>